/**
 * 初始化车次核销详情对话框
 */
var TTrainInfoDlg = {
    id: "TTrainCheckDisTable",	//表格id
    table: null,
    tTrainInfoData : {},
    lock: false
};


/**
 * 初始化表格的列
 */
var mA=1
var mB=1
var mC=1
var mD=1
var mE=1
TTrainInfoDlg.initColumn = function () {
    return [
        {title: '运单号', field: 'original_num',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '收货人', field: 'consignee',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '收货电话', field: 'receiving_phone',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '货物名称', field: 'goods_name',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '件数', field: 'goods_piece',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '送货地址', field: 'receivingAddress',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '提付', field: 'tf', width: 120,visible: true, align: 'center', valign: 'middle'},
        {title: '代收货款', field: 'ds',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '落地费', field: 'ldf',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '送货费', field: 'shf',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '搬运费', field: 'byf',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '仓储费', field: 'ccf',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '合计费用', field: 'sn',width: 120, visible: true, align: 'center', valign: 'middle',
            templet: function (d) {
                return parseFloat(d.tf)+parseFloat(d.ds)+parseFloat(d.shf)+parseFloat(d.byf)+parseFloat(d.ccf)+parseFloat(d.ldf);
            }},
        {title: '车费', field: 'cf',width: 120, visible: true, align: 'center', valign: 'middle'},
        {title: '回单要求', field: 'receipt',width: 100, visible: true, align: 'center', valign: 'middle'}

    ];
};
TTrainInfoDlg.summary = function() {
   alert()
}

/**
 * 清除数据
 */
TTrainInfoDlg.clearData = function() {
    this.tTrainInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TTrainInfoDlg.set = function(key, val) {
    this.tTrainInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TTrainInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
TTrainInfoDlg.close = function() {
    parent.layer.close(window.parent.TTrain.layerIndex);
}

/**
 * 收集数据
 */
TTrainInfoDlg.collectData = function() {
    this
    .set('id')
    .set('cash')
    .set('weixin')
    .set('alipay')
    .set('bank')
    .set('trainNo')
    ;
}

/**
 * 提交添加
 */
TTrainInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tTrain/add", function(data){
        Feng.success("添加成功!");
        window.parent.TTrain.table.refresh();
        TTrainInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tTrainInfoData);
    ajax.start();
}
var sc=null
TTrainInfoDlg.refStatus = function() {
    this.set('staus', "3");
    this.set('id', $('#id').val());
    sc= layer.confirm('确认核销？', {
        btn: ['取消', '确定']
    }, function(index, layero){
        layer.close(sc);
    }, function(index, layero){
        layer.close(sc);
        if(!TTrainInfoDlg.lock) {
            //防止多次点击
            TTrainInfoDlg.lock = true; // 锁定

            var ajax = new $ax(Feng.ctxPath + "/tTrain/orderZTHX", function (data) {
                Feng.success("操作成功!");
                window.parent.aTable.refresh();
                TTrainInfoDlg.close();
            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set(TTrainInfoDlg.tTrainInfoData);
            ajax.start();
        }

    });
}

/**
 * 提交修改
 */
TTrainInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    var reg = /^\-?[0-9]+\.?[0-9]*$/;
    if (!reg.test($('#cash').val().trim())  ) {
        Feng.error("现金金额不符合规范，请重新输入");
        return false;
    }
    if (!reg.test($('#weixin').val().trim())  ) {
        Feng.error("微信金额不符合规范，请重新输入");
        return false;
    }
    if (!reg.test($('#alipay').val().trim())  ) {
        Feng.error("支付宝金额不符合规范，请重新输入");
        return false;
    }
    if (!reg.test($('#bank').val().trim())  ) {
        Feng.error("银行卡金额不符合规范，请重新输入");
        return false;
    }
    var cash=parseFloat($('#cash').val().trim());
    var weixin=parseFloat($('#weixin').val().trim());
    var alipay=parseFloat($('#alipay').val().trim());
    var bank=parseFloat($('#bank').val().trim());
    var ys=parseFloat($('#ys').text().trim());
    this.set("ys",ys);
    if((cash+weixin+alipay+bank)!=ys){
        Feng.error("收支金额不等!");
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tTrain/updateDis", function(data){
        Feng.success("修改成功!");
        window.parent.aTable.refresh();
        TTrainInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tTrainInfoData);
    ajax.start();
}
var mDate=null;
TTrainInfoDlg.collectSum = function(m) {
    if(m){
        mDate=m;
    }

    var sum=0;
    var csum=0;
    for(var i=0;i<mDate.data.length;i++){
        sum=sum+parseFloat(mDate.data[i].tf)+parseFloat(mDate.data[i].ds)+parseFloat(mDate.data[i].shf)+parseFloat(mDate.data[i].byf)+parseFloat(mDate.data[i].ccf)+parseFloat(mDate.data[i].ldf);
        csum=csum+parseFloat(mDate.data[i].cf);

    }
    $('#cash').val(sum-csum);
    $('#hj').text(sum);
    $('#cf').text(csum);
    $('#ys').text(sum-csum);
}

var aTable = null
var mTable = null;
$(function () {
    var defaultColunms = TTrainInfoDlg.initColumn();
    aTable = new LYTable(TTrainInfoDlg.id, "/tTrain/detailDisList?batch_no="+$('#trainNo').val(), defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        TTrainInfoDlg.table = aTable.initCallBack(mTable,TTrainInfoDlg.collectSum);

    })


});
